`
https://leetcode.cn/problems/minimum-depth-of-binary-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var minDepth = function (root) {
  // 记录当前的最大深度
  let curMaxDepth = Infinity

  function dfs(root, depth) {
    // 遇到空节点直接返回
    if (root === null) return

    // 深度大于当前已记录的最大深度也直接返回
    if (depth > curMaxDepth) return

    // 遇到叶子节点，更新一下最大深度
    if (root.left === null && root.right === null) {
      curMaxDepth = Math.min(curMaxDepth, depth)
    }

    dfs(root.left, depth + 1)
    dfs(root.right, depth + 1)
  }

  dfs(root, 1)

  return curMaxDepth === Infinity ? 0 : curMaxDepth
};